<?php
class User_model extends CI_Model {

    var $TYPE_WATERING = 1;

    var $RESULT_PADDING = 1;
    var $RESULT_PASS    = 2;

    var $TYPE_STR = array(
            '', '水贴刷回复'
        );
    var $RESULT_STR = array(
            '', 'padding', 'pass'
        );

    var $tbs = '';

	function __construct()
    {
        parent::__construct();
        $tbs = json_decode($this->common->get_tbs());
        $this->tbs = $tbs;
    }

    public function get_tbs() {
        return $this->tbs->tbs;
    }

    public function get_uid_by_name($name = FALSE) {
    	if ($name == FALSE) {
    		$this->common->log('error' ,"user param not valid");
    		return -1;
    	}

    	$data = $this->db->where('name', $name)->get('user');

    	if ($data->num_rows() != 0) {
    		//存在这个用户
    		return $data->row()->id;
    	}

        if (filter_var($name, FILTER_VALIDATE_IP)) {
            return -1;
        }

    	//不存在这个用户，新建一个
    	$user_info = $this->common->fetch("http://tieba.baidu.com/home/get/panel?ie=utf-8&un={$name}");
    	$user_info = json_decode($user_info)->data;

        if ($user_info->id == NULL || $user_info->portrait == NULL)
            return -1;

    	$this->db->insert('user', array(
    		'id'	=> $user_info->id,
    		'name'	=> $name,
    		'portrait'	=> $user_info->portrait
    		));
        $this->common->log('info', "user: {$name} inserted");
    	return $user_info->id;
    }

    /**
    * 更新用户的最后回复时间
    */
    public function notify_reply_update($uid, $time) {
        $this->db->query('UPDATE user SET last_reply_time=GREATEST(last_reply_time, ?) WHERE id=?', array($time, $uid));
    }

    public function update_user_info($info) {
        if ($this->db->where('id', $info->id)->get('user')->num_rows() != 0) {
            //存在这个用户
            $this->db->update('user', array(
                'name_u'    => $info->name_u,
                'gender'    => $info->gender,
                'portrait'  => $info->portrait,
                'has_grade' => intval($info->has_grade),
                'grade_level'   => $info->grade_level,
                'grade_name'    => $info->grade_name,
                'grade_exp'     => $info->grade_exp,
                'fans_num'      => $info->fans_num,
                'is_concerned'  => intval($info->is_concerned),
                'is_private'    => intval($info->is_private),
                'outer_id'      => intval($info->outer_id),
                'bawu'          => intval($info->bawu)
                ), array(
                    'id'    => intval($info->id),
                    'last_fetch <=' => 'NOW()'
                ));
            $this->common->log('info', "user: {$info->name} with ID: {$info->id} updated");
        } else {
            //不存在这个用户，快新建它
            $this->db->insert('user', array(
                'id'        => $info->id,
                'name'      => $info->name,
                'name_u'    => $info->name_u,
                'gender'    => $info->gender,
                'portrait'  => $info->portrait,
                'has_grade' => intval($info->has_grade),
                'grade_level'   => $info->grade_level,
                'grade_name'    => $info->grade_name,
                'grade_exp'     => $info->grade_exp,
                'fans_num'      => $info->fans_num,
                'is_concerned'  => intval($info->is_concerned),
                'is_private'    => intval($info->is_private),
                'outer_id'      => intval($info->outer_id),
                'bawu'          => intval($info->bawu)
            ));
            $this->common->log('info', "user: {$info->name} with ID: {$info->id} insert");
        }
    }

    public function check_need_user() {
        $level = $this->db->where('key', 'warting_user_minimum_level')->get('options', 1)->row();
        if ($level == NULL) {
            $this->common->log('error', "undefined warting_user_minimum_level value in options table");
            return NULL;
        }
        $level = intval($level->value);

        $sql = 'SELECT u.id FROM user AS u
                WHERE
                    u.grade_level<=? AND
                        (SELECT e.id FROM event AS e
                            WHERE e.uid=u.id AND e.type=? LIMIT 1) IS NULL
                        OR u.last_reply_time>(SELECT MAX(time) FROM event AS e
                                                WHERE e.uid=u.id AND e.type=? GROUP BY uid)
                LIMIT 1;';
        $data = $this->db->query($sql, array($level, $this->TYPE_WATERING, $this->TYPE_WATERING))->row();
        if ($data == NULL)
            return NULL;
        return $data->id;
    }

    public function get_all_posts($uid) {
        // $lzl = $this->db->where('author_id', $uid)->get('lzl')->result();
        $thread_reply = $this->db->where('author_id', $uid)->get('thread_reply')->result();
        // array_map(function($val) { 
        //         $val->content = htmlspecialchars($val->content); 
        //         return $val;
        //     }, $lzl);
        array_map(function($val) { 
                $val->content = htmlspecialchars($val->content); 
                return $val;
            }, $thread_reply);
        return $thread_reply;
    }
}

?>